package com.lsht.web.controller.siwei;

import com.github.pagehelper.PageInfo;
import com.lsht.common.bo.IdListAcceptBo;
import com.lsht.common.core.controller.BaseController;
import com.lsht.common.core.domain.AjaxResult;
import com.lsht.common.core.domain.model.LoginUser;
import com.lsht.common.utils.ServletUtils;
import com.lsht.common.utils.poi.ExcelUtil;
import com.lsht.framework.web.service.TokenService;
import com.lsht.system.domain.siwei.PointShowhidden;
import com.lsht.system.domain.siwei.SiweiDevice;
import com.lsht.system.domain.siwei.SiweiPoint;
import com.lsht.system.domain.siwei.SiweiPointDetails;
import com.lsht.system.domain.siwei.business.SiweiPointBus;
import com.lsht.system.service.siwei.SiweiPointDetailsService;
import com.lsht.system.service.siwei.SiweiPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 点位管理Controller
 *
 * @author zyc
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/siwei/point")
@Api(value = "SiweiPoint", description = "点位管理", tags = "点位管理")
public class SiweiPointController extends BaseController {
    @Autowired
    private SiweiPointService siweiPointService;

    @Autowired
    private SiweiPointDetailsService siweiPointDetailsService;

    @Autowired
    private TokenService tokenService;

    /**
     * 新增点位管理
     */
    @PostMapping(ADD_FUNCTION_URL)
    @ApiOperation(value = ADD_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "点位管理")
//    @PreAuthorize("@ss.hasPermi('siwei:point:add')")
    public AjaxResult genAdd(SiweiPoint siweiPoint, MultipartFile file) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //获取部门ID
        Long deptId = loginUser.getUser().getDeptId();
        siweiPoint.setDeptId(deptId);
        return toAjax(siweiPointService.genAdd(siweiPoint, file));
    }

    /**
     * 新增点位管理app端
     */
    @PostMapping("addPointByApp")
//    @PreAuthorize("@ss.hasPermi('siwei:point:add')")
    @ApiOperation(value = "新增点位管理app端", httpMethod = "POST", response = AjaxResult.class, tags = "点位管理")
    public AjaxResult genAdd(@RequestBody SiweiPoint siweiPoint) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //获取部门ID
        Long deptId = loginUser.getUser().getDeptId();
        siweiPoint.setDeptId(deptId);
        return toAjax(siweiPointService.addPointByApp(siweiPoint));
    }

    /**
     * 删除点位管理
     */
    @PostMapping(DELETE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = DELETE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "点位管理")
//    @PreAuthorize("@ss.hasPermi('siwei:point:remove')")
    public AjaxResult genDeleteById(@RequestBody IdListAcceptBo idListAcceptBo) {
        List<String> idList = idListAcceptBo.getIdList();
        if (CollectionUtils.isNotEmpty(idList)) {
            return toAjax(siweiPointService.genDeleteById(idList));
        }
        return AjaxResult.error();
    }

    /**
     * 修改点位管理
     */
    @PostMapping(UPDATE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = UPDATE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "点位管理")
//    @PreAuthorize("@ss.hasPermi('siwei:point:edit')")
    public AjaxResult genUpdateById(@RequestBody SiweiPoint siweiPoint) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //获取部门ID
        Long deptId = loginUser.getUser().getDeptId();
        siweiPoint.setDeptId(deptId);
        return toAjax(siweiPointService.genUpdateById(siweiPoint));
    }

    /**
     * 查询点位管理列表
     */
    @PostMapping(QUERY_LIST_FUNCTION_URL)
    @ApiOperation(value = QUERY_LIST_FUNCTION, httpMethod = "POST", response = SiweiPoint.class, tags = "点位管理")
//    @PreAuthorize("@ss.hasPermi('siwei:point:list')")
    public AjaxResult genQueryList(@RequestBody SiweiPoint siweiPoint) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        boolean admin = loginUser.getUser().isAdmin();
        Long deptId = null;
        if (admin) {
            deptId = null;
        } else {
            //获取部门ID
            deptId = loginUser.getUser().getDeptId();
        }
        startPage();
        List<SiweiPoint> list = siweiPointService.genQueryList(siweiPoint, deptId);
        return AjaxResult.success(new PageInfo<>(list));
    }

    /**
     * 点位管理不分页列表
     */
    @PostMapping("notPagePointList")
    @ApiOperation(value = "点位管理不分页列表", httpMethod = "POST", response = SiweiPoint.class, tags = "点位管理")
    public AjaxResult notPagePointList(@RequestBody SiweiPoint siweiPoint) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        boolean admin = loginUser.getUser().isAdmin();
        Long deptId = null;
        if (admin) {
            deptId = null;
        } else {
            //获取部门ID
            deptId = loginUser.getUser().getDeptId();
        }
        List<SiweiPoint> list = siweiPointService.genQueryList(siweiPoint, deptId);
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询点位详情信息
     */
    @PostMapping(QUERY_BY_ID_FUNCTION_URL)
    @ApiOperation(value = QUERY_BY_ID_FUNCTION, httpMethod = "POST", response = SiweiDevice.class, tags = "点位管理")
//    @PreAuthorize("@ss.hasPermi('siwei:point:query')")
    public AjaxResult genQueryById(@RequestBody SiweiPoint siweiPoint) {
        SiweiPoint point = siweiPointService.genQueryById(siweiPoint);
        return AjaxResult.success(point);
    }

    /**
     * 大屏查询点位数量及较昨日新增数
     */
    @PostMapping("pointStatistics")
    @ApiOperation(value = "大屏查询点位数量及昨日新增数", httpMethod = "POST", response = SiweiPoint.class, tags = "点位管理")
    public AjaxResult pointStatistics(@RequestBody SiweiPoint siweiPoint) {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        boolean admin = loginUser.getUser().isAdmin();
//        Long deptId = null;
//        if (admin) {
//            deptId = null;
//        } else {
//            //获取部门ID
//            deptId = loginUser.getUser().getDeptId();
//        }
//        siweiPoint.setDeptId(deptId);
        HashMap map = siweiPointService.pointStatistics(siweiPoint);
        return AjaxResult.success(map);
    }

    /**
     * 大大屏按照片年，季，月查询隐患点位个数
     */
    @PostMapping("abnormalPointDetails")
    @ApiOperation(value = "大屏按照片年，季，月查询隐患点位个数", httpMethod = "POST", response = SiweiPoint.class, tags = "点位管理")
    public AjaxResult abnormalPointDetails(@RequestBody SiweiPoint siweiPoint) {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        boolean admin = loginUser.getUser().isAdmin();
//        if (admin) {
//            siweiPoint.setDeptId(null);
//        } else {
//            //获取部门ID
//            Long deptId = loginUser.getUser().getDeptId();
//            siweiPoint.setDeptId(deptId);
//        }

        HashMap map = siweiPointService.abnormalPointDetails(siweiPoint);
        return AjaxResult.success(map);
    }

    /**
     * 大屏按照所属区域分组展示异常点位
     */
    @PostMapping("abnormalPointByRegion")
    @ApiOperation(value = "大屏按照所属区域分组展示异常点位的个数", httpMethod = "POST", response = SiweiPoint.class, tags = "点位管理")
    public AjaxResult abnormalPointByRegion(@RequestBody SiweiPoint siweiPoint) {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        boolean admin = loginUser.getUser().isAdmin();
//        if (admin) {
//            siweiPoint.setDeptId(null);
//        } else {
//            //获取部门ID
//            Long deptId = loginUser.getUser().getDeptId();
//            siweiPoint.setDeptId(deptId);
//        }
        List<HashMap> map = siweiPointService.abnormalPointByRegion(siweiPoint);
        return AjaxResult.success(map);
    }

    /**
     * 大屏查询点位为隐患提报的数据
     */
    @PostMapping("pointByAbnormal")
    @ApiOperation(value = "大屏查询点位为隐患提报的数据", httpMethod = "POST", response = SiweiDevice.class, tags = "点位记录管理")
    public AjaxResult pointByAbnormal(@RequestBody SiweiPointDetails siweiPointDetails) {
        List<SiweiPointDetails> pointDetails = siweiPointDetailsService.pointByAbnormal(siweiPointDetails);
        return AjaxResult.success(pointDetails);
    }

    /**
     * 点位管理导入
     *
     * @param file
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping("/importPoint")
    @ApiOperation(value = "点位管理导入", httpMethod = "POST", response = SiweiPoint.class, tags = "点位管理")
    public AjaxResult importPoint(MultipartFile file) throws Exception {
        ExcelUtil<SiweiPointBus> util = new ExcelUtil<SiweiPointBus>(SiweiPointBus.class);
        List<SiweiPointBus> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = siweiPointService.importPoint(userList, operName);
        return success(message);
    }

    /**
     * 点位管理导出
     *
     * @param response
     * @param
     */
    @PostMapping("/exportPoint")
    @ApiOperation(value = "点位管理导出", httpMethod = "POST", response = SiweiPoint.class, tags = "点位管理")
    public void export(HttpServletResponse response, SiweiPoint siweiPoint) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //获取部门ID
        Long deptId = loginUser.getUser().getDeptId();
        List<SiweiPoint> list = siweiPointService.genQueryList(siweiPoint, deptId);
        ExcelUtil<SiweiPoint> util = new ExcelUtil<SiweiPoint>(SiweiPoint.class);
        util.exportExcel(response, list, "点位管理数据");
    }

    /**
     * 点位管理模板导出
     *
     * @param response
     */
    @GetMapping("/importTemplate")
    @ApiOperation(value = "点位管理模板导出", httpMethod = "GET", response = SiweiPoint.class, tags = "点位管理")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SiweiPointBus> util = new ExcelUtil<SiweiPointBus>(SiweiPointBus.class);
        util.importTemplateExcel(response, "点位管理模板");
    }


    /**
     * 点位显示隐藏
     */
    @PostMapping("editPointStatus")
    @ApiOperation(value = "点位显示隐藏", httpMethod = "POST", response = SiweiDevice.class, tags = "点位管理")
    public AjaxResult editPointStatus(@RequestBody PointShowhidden pointShowhidden) {
        List<String> idList = pointShowhidden.getIdList();
        if (CollectionUtils.isEmpty(idList)) {
            return AjaxResult.error("ID不能为空");
        }
        return toAjax(siweiPointDetailsService.editPointStatus(pointShowhidden));
    }

    /**
     * 数据分析查询年度季度月度增长比例
     */
    @PostMapping("getGrowthRate")
    @ApiOperation(value = "数据分析查询年度季度月度增长比例", httpMethod = "POST", response = SiweiDevice.class, tags = "点位管理")
    public AjaxResult getGrowthRate(@RequestBody SiweiPoint siweiPoint) {
        return siweiPointDetailsService.getGrowthRate(siweiPoint);
    }
}
